package com.mdp.im.core.publish.action.impl;

import com.alibaba.fastjson.JSONObject;
import com.mdp.core.err.BizException;
import com.mdp.im.core.entity.ImGroup;
import com.mdp.im.core.publish.action.AbstractPublishAction;
import com.mdp.im.core.publish.api.action.PublishAction;
import com.mdp.im.core.publish.api.Publisher;
import com.mdp.im.core.publish.api.StorePublisher;
import com.mdp.im.core.service.ImGroupMessageService;
import com.mdp.im.core.service.ImGroupService;
import com.mdp.im.core.service.ImGroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 群组被删除处理器
 * 
 * @author chenyc
 * @date 2018/6/12
 */
@Service("pushChannelGroupRemoveAction")
public class ChannelGroupRemoveAction extends AbstractPublishAction implements PublishAction {

	@Autowired
	ImGroupService groupService;
	@Autowired
	ImGroupUserService groupUserService;
	
	@Autowired
	ImGroupMessageService groupMessageService;
	
	@Autowired
	Publisher publisher;


	@Autowired
	StorePublisher storePublisher;
	
    /**
     * 处理消息
     * 
     * 
     * @param message
     */
    @Override
    public void doAction(JSONObject message) {
    	String groupId=message.getString("groupId");
    	ImGroup g=new ImGroup();
    	g.setId(groupId);
    	g.setGstatus("2");//解散群
    	g.setLastUpdateTime(new Date()); 
    	String sendUserid=message.getString("sendUserid");
	   	 if(StringUtils.isEmpty(sendUserid)) {
			 throw new BizException("只有群主可以解散群");
		 }
    	 ImGroup groupDb=groupService.getImGroupByGroupId(groupId);
         if(groupDb!=null) { 
        	 
        	 if(!sendUserid.equals(groupDb.getCuserid())) {
        		 throw new BizException("只有群主可以解散群");
        	 }
              message.put("groupName", groupDb.getGroupName()); 
              message.put("categoryId", groupDb.getCategoryId());
              message.put("cuserid", groupDb.getCuserid());
              message.put("cusername", groupDb.getCusername());
              message.put("allowJoin", groupDb.getAllowJoin());
              message.put("allowSpeechTime", groupDb.getAllowSpeechTime()); 
              message.put("gstatus", "2");
              groupService.updateSomeFieldByPk(g);
              groupService.clearCahceGroup(groupId);
              publisher.publishGroupMessage(groupId, message.toJSONString());
          }  
    	//groupService.deleteByPk(g);
    	
       
    }

}
